

;___ GUI Code _______________________________________________________________________________________

	
	;~ ~~~~~~~~~~~~~
;~ ;SUPER SHORTCUT

SUPER_SHORTS:
	
	COMMAND := USER_INI.UserHotkeys[A_ThisHotkey]
	execute(COMMAND)

return




MAIN_TRIGGER:	;{
	
	If (WinExist("c0bra Main GUI"))
	{
		if WinActive("c0bra Main GUI")
			SLR_GUI()
		else
		{
			Gui, Destroy
			goto, Gui
		}
	}
	else
	{
		IfWinExist SLR
		{
			IfWinActive SLR
				goto Gui
			else
				WinActivate SLR
		}
		else
			goto Gui
	}
	
	;}
return

	
;~~~~~~~~~
;RELOAD ME

RELOADME:		
	Reload			
return


;~~~~~~~~~~~~~
;MAIN GUI CALL

	Gui:
	
	;~ CURR_GUI := 1
	
	Gui, 1:Destroy
	Gui, 2:Destroy ; Kill SLR gui if it exists
	
	MouseGetPos, XPOS, YPOS ; get mouse position to show gui
	
	;~~~~~~~~~~~~~
	;SET VARIABLES
	
		GUIoLDpOS:
	
		VARSET()
		Array := Object()
		
		Gui, Color, % USER_INI.Colors.GuiBackColor ; gui background color
		
	;~~~~~~~
	;HOTKEYS
	
		Hotkey, IfWinActive, c0bra
		Hotkey, Enter, ButtonPress ; search google if enter button pressed
		Hotkey, IfWinActive, c0bra
		Hotkey, NumpadEnter, ButtonPress ; search google if enter button pressed
		Hotkey, IfWinActive, c0bra
		Hotkey, ^Enter, ButtonPress ; go to website in search
		Hotkey, IfWinActive, c0bra
		Hotkey, ^NumpadEnter, ButtonPress ; go to website in search
		
	
	;~~~~~~~~
	;GUI CALL

		gui, -caption +ToolWindow +AlwaysOnTop
		gui, margin, %GUI_SPACING%, %GUI_SPACING%
		
		;~~~~~~~~~~~~~~~~~~~~~~~~~~
		;GOOGLE SEARCH - IF ENABLED
			
			if (SEARCH)
			{
				Gui, font, s%SEARCH_TEXT_SIZE% w400
				Gui, Add, Edit, x%GUI_SPACING% y%GUI_SPACING% w%SEARCH_WIDTH% h%SEARCH_HEIGHT% 0x200 vGSEARCH,
				Gui, Add, Text, xp yp-1 w%SEARCH_TEXT_WIDTH% h%SEARCH_HEIGHT%-2 0x200 Center +backgroundtrans hwndBackSearch, %SEARCH_BACK_TEXT%
				
				gui, font, s%TEXT_SIZE% w%TEXT_BOLD% ; gui button font size
				
				Array := GET_COLOR(USER_INI.MainButtons.GO, 1)
				Gui, Add, text, x%GO_X% y%GUI_SPACING% w%GO_WIDTH% h%SEARCH_HEIGHT% 0x200 Center gButtonPress hwndGO, GO
					CTLCOLORS.Attach(GO, Array[2], Array[1])
			}
			
		;~~~~~~~~~~~~~~~
		;ADD GUI BUTTONS
		
			gui, font, s%TEXT_SIZE% w%TEXT_BOLD% ; gui button font size

			StringReplace, MAIN_BUTTONS, MAIN_BUTTONS, GO`n ;Dont add the go button for the search b/c its only used for colors

			loop, Parse, MAIN_BUTTONS, `n ; get count of button labels for gui adds below
				KEY_COUNT := A_Index

			loop, Parse, MAIN_BUTTONS, `n ; create gui based on labels
			{
				A_HWND := "back" A_Index
				
				
				if (A_Index = 1) ; first key
				{
					Array := GET_COLOR(USER_INI.MainButtons[A_LoopField], 1)
					Gui, Add, text, x%GUI_SPACING% y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center gButtonPress hwnd%A_HWND%, % A_LoopField
						CTLCOLORS.Attach(%A_HWND%, Array[2], Array[1])
					continue
				}
				
				else if (mod(A_Index, 2)) ; odd key
				{
					if (A_Index = KEY_COUNT) ; odd last key
					{
						LAST_X := GUI_SPACING + (BUTTON_WIDTH / 2) ; get x position for last button
						Array := GET_COLOR(USER_INI.MainButtons[A_LoopField], 1)
						Gui, Add, text, x%LAST_X% y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center gButtonPress hwnd%A_HWND%, % A_LoopField
							CTLCOLORS.Attach(%A_HWND%, Array[2], Array[1])
						continue
					}
					
					Array := GET_COLOR(USER_INI.MainButtons[A_LoopField], 1)
					Gui, Add, text, x%GUI_SPACING% y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center gButtonPress hwnd%A_HWND%, % A_LoopField
						CTLCOLORS.Attach(%A_HWND%, Array[2], Array[1])
					continue
				}
				
				else ; even key
				{
					Array := GET_COLOR(USER_INI.MainButtons[A_LoopField], 1)
					Gui, Add, text, x+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center gButtonPress hwnd%A_HWND%, % A_LoopField
						CTLCOLORS.Attach(%A_HWND%, Array[2], Array[1])
					continue
				}
			}
			
		;~~~~~~
		;FOOTER
			
			if FOOTER_SHOW
			{
				Gui, font, s7 cWhite
				Gui, Add, Text, x%GUI_SPACING% y+5 w%FOOTER_WIDTH% h10 0x200 Center hwndFOOTER, c0bra v%VERSION%
			}
			
		gui, show, x%XPOS% y%YPOS%, c0bra Main GUI

	return



	;~~~~~~~~~~
	;GUI 2 CALL
		
	SIDE_GUI(NEWGUI)
	{
		global
		
		CURR_GUI := A_gui
		NEXT_GUI := CURR_GUI = 1 ? CURR_GUI + 2 : CURR_GUI + 1
		
		SetTitleMatchMode, 3
		IfWinExist, %NEWGUI%
		{
			gui, %NEXT_GUI%:Destroy
			return
		}
		SetTitleMatchMode, 1
		
		PREV_GUI := CURR_GUI
		CURR_GUI := NEXT_GUI
		
		;~~~~~~~~~~~~~
		;SET VARIABLES
		
			VARSET()
		
		Gui, Submit, nohide
		Gui, +LastFound
		WinGetPos,x,y,w,h

		GuiControlGet, CONTROL_POS, Pos, %A_GuiControl%
		
		if (PREV_GUI = 1)
			ON_LEFT := ON_LEFT(CONTROL_POSX)
		
		if ON_LEFT
		{
			XPOS := x - (2 * GUI_SPACING) - BUTTON_WIDTH
			YPOS := y + CONTROL_POSY - GUI_SPACING
		}
		else
		{
			XPOS := x + CONTROL_POSX + BUTTON_WIDTH + GUI_SPACING
			YPOS := y + CONTROL_POSY - GUI_SPACING
		}
		
		Gui, %CURR_GUI%:Color, % USER_INI.Colors.Gui2BackColor ; gui 2 background color
		Gui, %CURR_GUI%:-caption +ToolWindow +Owner%PREV_GUI%
		gui, %CURR_GUI%:margin, %GUI_SPACING%, %GUI_SPACING%
		gui, %CURR_GUI%:Font, s%TEXT_SIZE% w%TEXT_BOLD%
		
		;Bookmarks
		if (NEWGUI = "Bookmarks")
		{
			MyFavs := A_MyDocuments "\..\Favorites\Links"
			USER_INI.Delete("Bookmarks")
			
			theKeys := USER_INI.Keys("Parents")
			Loop, Parse, theKeys, `n
			{
				if (USER_INI.Parents[A_LoopField] = "Bookmarks")
				{
					USER_INI.Delete("Parents", A_LoopField)
					USER_INI.Delete("NoChildren", A_LoopField)
					USER_INI.Delete("NoDelete", A_LoopField)
				}
			}
			
			;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			;LOOP THROUGH FAVORITES FOLDER FOR KEY, VALUE, AND COUNT
				loop, %MyFavs%\*.url
				{
					if (A_LoopFileExt = "URL")
					{
						THE_BOOKMARK := RegExReplace(A_LoopFileName, "i)\.[^.]*$")
						THE_BOOKMARK_PATH := A_LoopFileLongPath
						
						BParent := GetParent("Bookmarks", USER_INI)
						RegExMatch(USER_INI[BParent][NEWGUI], "i)\{.*\}", COLORS)
						
						MenuIniWrite("Bookmarks", THE_BOOKMARK, THE_BOOKMARK, "B", COLORS, 1, 1)
						;~ USER_INI.Bookmarks[THE_BOOKMARK] := " [B] " COLORS " " THE_BOOKMARK
						;~ USER_INI.Parents[THE_BOOKMARK] := "Bookmarks"
						;~ USER_INI.NoChildren[THE_BOOKMARK] := THE_BOOKMARK
						;~ USER_INI.NoDelete[THE_BOOKMARK] := THE_BOOKMARK
						
						A_HWND := "book" A_Index
						Array := GET_COLOR(USER_INI.MainButtons.Bookmarks, 1)
						Gui, %CURR_GUI%:Add, text, y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center g3ButtonPress hwnd%A_HWND%, % THE_BOOKMARK
							CTLCOLORS.Attach(%A_HWND%, Array[2], Array[1])
					}
				}
			
			USER_INI.Save(USER_SETTINGS)
		}
		
		;not bookmarks
		else
		{
			NEWGUI_KEYS := USER_INI.Keys(NEWGUI)
			Loop, Parse, NEWGUI_KEYS, `n
			{
				A_HWND := "side" A_Index
				Array := GET_COLOR(USER_INI[NEWGUI][A_LoopField], 1)
				Gui, %CURR_GUI%:Add, text, y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% 0x200 Center g3ButtonPress hwnd%A_HWND%, % A_LoopField
					CTLCOLORS.Attach(%A_HWND%, Array[2], Array[1])
			}
		}
		
		Gui, %CURR_GUI%:Show, x%XPOS% y%YPOS%, %NEWGUI%
		return 
	}
	
		
	SLR_GUI()
	{
		global
		
		Gui, 1:Destroy
		
		;~~~~~~~~~~~~~
		;SET VARIABLES
		
			VARSET()
			
		Array := Object()
		
		MouseGetPos, MouseX1, MouseY1
		
		gui, 2:font, s%TEXT_SIZE% w%TEXT_BOLD%
		Gui, 2:+toolwindow -caption
		Gui, 2:Color, FF0000
		Gui, 2:Margin, %GUI_SPACING%, %GUI_SPACING%
		
		theKeys := trim(USER_INI.Keys("SLR"))
		Loop, Parse, theKeys, `n
		{
			Array := GET_COLOR(trim(USER_INI.SLR[A_LoopField]), 1)
			if (A_Index = 1)
				Gui, 2:Add, text, x%GUI_SPACING% y%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% hwnd%A_LoopField% 0x200 Center gSLR, %A_LoopField%
			else
				Gui, 2:Add, text, y+%GUI_SPACING% w%BUTTON_WIDTH% h%BUTTON_HEIGHT% hwnd%A_LoopField% 0x200 Center gSLR, %A_LoopField%
			
				CTLCOLORS.Attach(%A_LoopField%, Array[2], Array[1])
		}
		
		Gui, 2:Show, x%MouseX1% y%MouseY1%, SLR
	}
	
	
	SLR:
		Gui, Submit
		if !(A_GuiControl = "Cancel")
		{
			MsgBox,4,, SAVE ALL YOUR WORK BEFORE HITTING YES!!`n`nAre you sure you want to %A_GuiControl%?
			IfMsgBox yes
			{
				Gui, 1:Destroy
				Shutdown, % A_GuiControl = "Shutdown" ? "13" : A_GuiControl = "Restart" ? "6" : "0"
			}
			Else
				return
		}
	
	return


	
			
	GuiSize:
	2GuiSize:
	3GuiSize:
	4GuiSize:
	5GuiSize:
	   If (A_EventInfo != 1)
	   {
		  Gui, %A_Gui%:+LastFound
		  WinSet, ReDraw
	   }
	Return



	GuiClose:
	GuiEscape:
	2GuiClose:
	2GuiEscape:	
	3GuiClose:
	3GuiEscape:
	4GuiClose:
	4GuiEscape:
	5GuiClose:
	5GuiEscape:	
		CTLCOLORS.free()
		Gui, %A_Gui%:Destroy
	return



;___ RIGHT-CLICK MENU ________________________________________________________________________________


GuiContextMenu:
2GuiContextMenu:
3GuiContextMenu:
4GuiContextMenu:
5GuiContextMenu:
	MouseGetPos, XPOS, YPOS
	me := A_GuiControl	
	meDisp := RegExReplace(me, "i)(.+)s$", "$1(s)")
	meAdd  := RegExReplace(me, "i)(.+)s$", "$1")
	
	mainButton := USER_INI.Keys("MainButtons")
	noChildren := USER_INI.Keys("NoChildren")
	noDelete := USER_INI.Keys("NoDelete")
	
	theKeys := USER_INI.Keys("ButtonTypes")
	Loop, parse, theKeys, `n
	{
		if (A_Index = 1)
			pipelist := trim(USER_INI.ButtonTypes[A_LoopField])
		else
			pipelist := pipelist "|" trim(USER_INI.ButtonTypes[A_LoopField])
	}
	
	BookKeys := USER_INI.Keys("Bookmarks")
	if (instr(BookKeys, me))
		return
			
	; Common top of menu	
	Menu, Title, Add
	Menu, Title, DeleteAll		
	menu, title, add, Cancel, cancelMenuItem
	menu, title, default, cancel
	

	If (instr(mainButton, me))
	{
		Menu, Title, Add
		Menu, SubAdd, Add
		Menu, SubAdd, DeleteAll
		loop, parse, pipeList, |
			Menu, SubAdd, Add, %A_LoopField%, QuickEditMenu
		
		Menu, Title, Add, Add Main Button, :SubAdd
		Menu, Title, Add
	}
	
	

	if !(instr(noChildren, me) || me = "GO" || me = "Bookmarks")
	{
		Menu, SubAdd1, Add
		Menu, SubAdd1, DeleteAll
		loop, parse, pipeList, |
			Menu, SubAdd1, Add, %A_LoopField%, QuickEditMenu
		
		Menu, Title, Add, Add to %me%, :SubAdd1
	}
	
	Menu, Title, Add, Change %me% Color, QuickEditMenu
	
	if (me != "GO")
	{
		if !(instr(NoDelete, me))
			Menu, Title, Add, Delete %me% button, QuickEditMenu
		
		;~ Menu, Title, Add, Edit %meDisp%, QuickEditMenu
		
		if (instr(mainButton, me) || me = "GO")
			Menu, Title, Add
	}
	
	if (instr(mainButton, me) || me = "GO")
	{
		Menu, SubSearch, Add
		Menu, SubSearch, DeleteAll
		Menu, SubSearch, Add, Show Search Bar, QuickEditMenu
		Menu, SubSearch, % USER_INI.GuiInfo.Search ? "Check" : "Uncheck", Show Search Bar
		Menu, SubSearch, Add, % "Change Text: " USER_INI.GuiInfo.BackText, QuickEditMenu
		Menu, SubSearch, Add
		Menu, SubSearch, Add, Show Footer, QuickEditMenu
		Menu, SubSearch, % USER_INI.GuiInfo.Footer ? "Check" : "Uncheck", Show Footer
		Menu, Title, Add, c0bra Options, :SubSearch
	}
	
	Menu, Title, Show, x%XPOS% y%YPOS%
	
return
		
		
CancelMenuItem:
return


QuickEditMenu:

	If (A_ThisMenuItem = "Outlook" || A_ThisMenuItem = "Program")
	{
		commandLetter := "P"
		
		if (A_ThisMenuItem = "Outlook")
		{
			programName := "Outlook"
			programPath := "Outlook"
		}
		else
		{
			Gui +LastFound +OwnDialogs +AlwaysOnTop
			InputBox, programName, Button Title, What is the name of the program?
			Gui +LastFound +OwnDialogs +AlwaysOnTop
			FileSelectFolder, programPath,, Select the program exe that the %n% button will open
		}
		
		colorSet := ColorPicker()
		
		MenuIniWrite(me, programName, programPath, commandLetter, colorSet, 1, 0)
		
		Gui, Destroy
		goto, guiOldPos
	}
	
	else if (A_ThisMenuItem = "Folder")
	{
		Gui +LastFound +OwnDialogs +AlwaysOnTop
		InputBox, n, Button Title, What is the button Title?
		Gui +LastFound +OwnDialogs +AlwaysOnTop
		FileSelectFolder, folderPath,, Select the folder that the %n% button will link to
		
		USER_INI[me][folderName] := "[E] " folderPath
		
		
		IniWrite, %theFolder%, % USER_SETTINGS, %me%, %n%
		IniWrite, %me%, % USER_SETTINGS, Parents, %n%
		IniWrite, %n%, % USER_SETTINGS, NoChildren
		
		Gui, Destroy
		goto, guiOldPos
	}
	
	else if (A_ThisMenuItem = "Show Search Bar")
	{
		;~ theSearch := USER_INI.GuiInfo.Search ? 0 : 1
		IniWrite, % USER_INI.GuiInfo.Search ? 0 : 1, % USER_SETTINGS, GuiInfo, Search
		Gui, Destroy
		goto, guiOldPos
	}
	
	else if (A_ThisMenuItem = "Change Text: " USER_INI.GuiInfo.BackText)
	{
		InputBox, newText, Search Text, Enter text to be shown in search box.
		IniWrite, %newText%, % USER_SETTINGS, GuiInfo, BackText
		Gui, Destroy
		goto, guiOldPos
	}
	
	else if (A_ThisMenuItem = "Footer")
	{
		IniWrite, % USER_INI.GuiInfo.Footer ? 0 : 1, % USER_SETTINGS, GuiInfo, Footer
		Gui, Destroy
		goto, guiOldPos
	}
	
	else if (InStr(A_ThisMenuItem, "Delete"))
	{
		MsgBox, 262196, Are you sure?, Delete this button? 
		ifmsgbox, Yes
		{
			IniRead, theParent, % USER_SETTINGS, Parents, %me%
			IniDelete, % USER_SETTINGS, %theParent%, %me%
			IniDelete, % USER_SETTINGS, Parents, %me%
			
			Gui, Destroy
		}
	}
	
	else
	{
		msgbox, 4144,, Sorry!`n`nThis feature is still under construction...
	}
return


NoChildren(USER_SETTINGS)
{
	MsgBox, 262180, Button Children, Will this button have sub-buttons?
	IfMsgBox, No
	{
		IniRead, temp, % USER_SETTINGS, NoChildren
		If !(instr(temp, me))
			IniWrite, %n%, % USER_SETTINGS, NoChildren
	}
}
		
	;~~~~~~~~~~~~~~~~~~~~~~~~
	;RIGHT CLICK MENU HANDLER
		
		TitleHandler:
		
				Run, % A_ThisMenuItem = "User Settings" ? "explorer.exe " USER_SETTINGS 
						: A_ThisMenuItem = "User Functions" ? AHKEDITOR " " USER_FUNCTIONS 
						: "explorer.exe " MGR_COMMANDS
				Gui, Destroy
		
		return


	;~~~~~~~~~~~~~~~~~~
	;BUTTON PRESS LABEL
		
		ButtonPress:
		
			if !(WinExist(A_GuiControl))
				Gui, 3:Destroy ; kill gui 3 before in case another gui is selected
			
			GuiControlGet, controlhwnd, Hwnd, %A_GuiControl% ; hwnd of control pressed
			
			
			;~~~~~~~~~~~~~
			;GOOGLE SEARCH
				
				IfWinActive, c0bra Main GUI
				{
					if (controlhwnd = go || !A_GuiControl)
					{
						if SEARCH
						{
							Gui, Submit
							
							if instr(A_ThisHotkey, "^") ; if control is held for a website
							{
								Run % instr(GSEARCH, ".com") ? "www." GSEARCH : "www." GSEARCH ".com"
								Gui, Destroy
								return
							}
							
							Google(GSEARCH) ; regular google search
							Gui, Destroy
							return
						}
					}
				}
				
			Gui, Submit, NoHide
				
			;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			;USER COMMANDS FROM SETTINGS INI

				COMMAND := USER_INI.MainButtons[A_GuiControl]
					
				Execute(COMMAND)
			
		return


	;~~~~~~~~~~~~~~~~~~~~~
	;SIDE GUI BUTTON PRESS
		
		3ButtonPress:

			NEXT_KILL := A_GUI + 1
			
			SetTitleMatchMode, 3
			if !(WinExist(A_GuiControl))
				Gui, %NEXT_KILL%:Destroy ; kill gui 2 before in case another gui is selected
			SetTitleMatchMode, 1
			WinGetActiveTitle, A_TITLE ; get title of gui 2
			
			if (A_TITLE = "Bookmarks")
			{
				MyFavs := A_MyDocuments "\..\Favorites\Links"
				Run, % "explorer.exe " MyFavs "\" A_GuiControl ".url"
				Gui, 1:Destroy
				return
			}

			COMMAND := USER_INI[A_TITLE][A_GuiControl] ; set command var from user settings
			
			Execute(COMMAND) ; execute command
		
		return
		
		
	;~~~~~~~~~~~~~~~~~
	;EXIT APP FOR TRAY
		closer:
			ExitApp
		return
		
		
	;~~~~~~~~~~~~~~~~~~
	;SETTINGS FOR TRAY
		MeSettings:
			Gui, Destroy
			Run % USER_SETTINGS
		return
	
	
	;~~~~~~~~~
	;TRAY TEXT
		TrayText:
			goto Gui
		return
	
	
	
	
	
	